/*******************************************************************************
 * Copyright (c) 2008, 2009 Fabio Mauro - Alberto Terraneo.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.2
 * or any later version published by the Free Software Foundation.
 *     
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU Free Documentation License for more details.
 *     
 * You should have received a copy of the GNU Free Documentation 
 * License along with this distribution; if not, send a email to authors:
 * aterraneo <at> users.sourceforge.net
 * bigmoby09 <at> users.sourceforge.net
 *******************************************************************************/
package it.blueocean.acanto.web.platform.providers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.IFilterStateLocator;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

public class FilteredAcantoProvider<T> extends SortableDataProvider<T> implements IFilterStateLocator
{
	private static final long serialVersionUID = 3116097506039962811L;

	List<T> list = new ArrayList<T>();
	
	T object;

	public FilteredAcantoProvider(List<T> listData, String property)
	{
		setSort(property, true);
		list.addAll(listData);
	}

	public Iterator<T> iterator(int first, int count)
	{

		List<T> newList = new ArrayList<T>();

		newList.addAll(this.list.subList(first, first + count));

		final String sortColumn = this.getSort().getProperty();
		final boolean ascending = this.getSort().isAscending();

		Collections.sort(newList, new Comparator<Object>()
		{

			@SuppressWarnings("unchecked")
			@Override
			public int compare(Object obj1, Object obj2)
			{
				PropertyModel model1 = new PropertyModel(obj1, sortColumn);
				PropertyModel model2 = new PropertyModel(obj2, sortColumn);

				Object modelObject1 = model1.getObject();
				Object modelObject2 = model2.getObject();

				int compare = ((Comparable<Object>) modelObject1).compareTo(modelObject2);

				if(!ascending)
					compare *= -1;

				return compare;
			}
		});

		return newList.iterator();
	}

	public IModel<T> model(final T object)
	{
		return new AbstractReadOnlyModel<T>()
		{
			private static final long serialVersionUID = -1671473121288085835L;

			@Override
			public T getObject()
			{
				return object;
			}
		};
	}

	public int size()
	{
		return list.size();
	}

	@Override
	public Object getFilterState()
	{
		System.out.println("called getFilterState : " + object);
		return object;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void setFilterState(Object state)
	{
		object = (T) state;
		System.out.println("called ISortableDataProvider : " + object);		
	}

}
